﻿using System;
using System.Collections.Generic;
using System.Text;
using RS.DOM.Common;

namespace RS.DOM.Types
{
    public class MutableCollection : List<Mutable>
    {
        public bool Contains(string text)
        {
            foreach (Mutable mutable in this)
                if (mutable.Text == text)
                    return true;

            return false;
        }
    }

    public class Mutable : Token
    {
        public Mutable(Context context, string text)
            : base(context)
        {
            base.Text = text;
            base.TokenType = TokenType.Mutable;
            base.Result = new List<Token>();
            m_selectedIndex = 0;
        }

        public override Token Resolve()
        {
            if (!base.IsResolved)
            {
                int len = 0;

                if (this.Context.Group != null)
                {
                    len = this.Context.Group.Numbers.Result.Count;
                }
                else
                {
                    if (this.Text == "i")
                    {
                        len = this.Context.Document.Area.Width;
                        len = 100;
                    }
                    else if (this.Text == "j")
                    {
                        len = this.Context.Document.Area.Height;
                        len = 100;
                    }
                }

                for (int i = 0; i < len; i++)
                {
                    this.Result.Add(new Number(this.Context, i));
                }

                base.Resolve();
            }

            return this;
        }

        public override Token Create()
        {
            Mutable result = new Mutable(this.Context, this.Text);
            foreach (Token token in this.Result)
            {
                result.Result.Add(token.Copy());
            }

            return result;
        }

        private int m_selectedIndex;

        public int SelectedIndex
        {
            get
            {
                return m_selectedIndex;
            }

            set
            {
                m_selectedIndex = value;
            }
        }

        public Token SelectedToken
        {
            get
            {
                return this.Result[this.SelectedIndex];
            }
        }

        public List<Token> Result
        {
            get
            {
                return (List<Token>)base.Result;
            }
        }
    }
}
